package com.noahplat.user.controller;

import com.noahplat.common.utils.Result;
import com.noahplat.user.pojo.User;
import com.noahplat.user.utils.PasswordDao;
import com.noahplat.user.utils.UserDao;
import com.noahplat.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

@RequestMapping("/api/user")
@RestController
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 作者：李景星
     * 描述：通过用户名获取用户信息
     * 时间：2020-10-31
     *
     * @param username
     */
    @GetMapping("/queryUserByUsername/{username}")
    public Result<User> queryUserByUsername(@PathVariable("username") String username) {
        return userService.queryUserByUsername(username);
    }

    /**
     * 根据Id查询用户信息
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:UserController:findUserById')")
    @GetMapping("/findUserById")
    public Result<User> findUserById(Long id) {
        return userService.findUserById(id);
    }

    /**
     * 根据Id修改用户信息
     *
     * @param user
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:UserController:modifyUserById')")
    @PostMapping("/modifyUserById")
    public Result modifyUserById(@RequestBody User user) {
        return userService.updateUserById(user);
    }

    /**
     * 根据token修改用户信息
     *
     * @author 李东
     * @time 2021-02-25 13:13
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:UserController:updateUserInfo')")
    @PostMapping("/updateUserInfo")
    public Result updateUserInfo(String name, String email, String phone, @NotNull(message = "用户ID不能为空 ") @Min(value = 1, message = "用户Id不能为空") @RequestParam("uid") Long uid) {
        return userService.updateUserInfo(name, email, phone, uid);
    }

    /**
     * 修改用户密码
     *
     * @author 李东
     * @time 2021-02-25 13:13
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:UserController:password')")
    @PostMapping("/password")
    public Result updatePassword(@NotNull(message = "原密码不能为空") String oldPassword, @NotNull(message = "新密码不能为空") String password, @Min(value = 1, message = "用户Id不能为空") Long uid) {
        return userService.updatePassword(oldPassword, password, uid);
    }

    /**
     * 用户注册
     *
     * @author 李东
     * @time 2021-02-25 16:25
     */
    @PostMapping("/register")
    public Result register(@Validated @RequestBody UserDao userDao) {
        return userService.register(userDao);
    }

    /**
     * 找回密码操作
     *
     * @author 李东
     * @time 2021-02-27 16:25
     */
    @PostMapping("/resetPassword")
    public Result resetPassword(@Validated @RequestBody PasswordDao passwordDao) {
        return userService.resetPassword(passwordDao);
    }

    /**
     * 更新头像
     *
     * @param avatar
     * @param uid
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:UserController:updateAvatar')")
    @PostMapping("/updateAvatar")
    public Result updateAvatar(@NotEmpty(message = "图片不能为空") String avatar, @Min(value = 1, message = "用户id不能为空") Long uid) {
        return userService.updateAvatar(avatar, uid);
    }

    @PreAuthorize("hasAuthority('noahplat-service-user:UserController:updatePhone')")
    @PostMapping("/updatePhone")
    public Result updatePhone(@NotEmpty(message = "手机号不能为空") String phone, @NotEmpty(message = "短信验证码不能为空") String smsCode, @Min(value = 1, message = "用户id不能为空") Long uid) {
        return userService.updatePhone(phone, smsCode, uid);
    }

    /**
     * 条件查询用户信息
     *
     * @param page
     * @param limit
     * @param stid
     * @param acctid
     * @param name
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:UserController:getUser')")
    @GetMapping("/getUser")
    public Result getUser(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10")Integer limit, @Min(value = 1, message = "系统id不能为空") Long stid, @Min(value = 1, message = "账套id不能为空") Long acctid, String name) {
        return userService.getUser(page, limit, stid, acctid, name);
    }

    /**
     * 根据ID查询用户信息
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:UserController:getUserById')")
    @GetMapping("/getUserById")
    public Result getUserById(@Min(value = 1, message = "用户id不能为空") Long id) {
        return userService.getUserById(id);
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:UserController:updateUserRoleById')")
    @PutMapping("/updateUserRoleById")
    public Result updateUserRoleById(@RequestBody User user) {
        return userService.updateUserRoleById(user);
    }

    /**
     * 根据ID删除角色
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:UserController:delUser')")
    @PutMapping("/delUser")
    public Result delUser(Long[] id) {
        return userService.delUser(id);
    }


    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:UserController:addUser')")
    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user) {
        return userService.addUser(user);
    }

    /**
     * 通过手机号查找用户
     * @param phone
     * @return
     */
    @GetMapping("/getUserByPhone")
    public Result getUserByPhone(Long stid,String phone){
        return userService.getUserByPhone(stid,phone);
    }

    /**
     * 分页查询企业管理账户
     * @param stid
     * @param page
     * @param limit
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:UserController:getProUser')")
    @GetMapping("/getProUser")
    public Result getProUser(Long stid,@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10")Integer limit){
        return userService.getProUser(stid,page,limit);
    }

    /**
     * 初始化管理用户
     * @param user
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:UserController:addAdminUser')")
    @PostMapping("/addAdminUser")
    public Result addAdminUser(@RequestBody User user){
        return userService.addAdminUser(user);
    }

    /**
     * 查询用户的数量
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:UserController:getCount')")
    @GetMapping("/getCount")
    public Result getCount(){
        return userService.getCount();
    }

}
